package top.zkh.login;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.java1234.dao.UserDao;
import com.java1234.util.DbUtil;

public class Register extends HttpServlet{
	private DbUtil dbUtil = new DbUtil();
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		req.setCharacterEncoding ("utf-8");
		resp.setContentType("text/html;carset=utf-8");
		Map<String,String[]> map =req.getParameterMap();
		Connection con =null;
		PreparedStatement ps =null;
		if(map.get("user")[0].equals("a")||map.get("user")[0].equals("k")||map.get("user")[0].equals("q")) {
			req.getRequestDispatcher("erroruser.jsp").forward(req, resp);
		}
		try {
			con = dbUtil.getCon();
			String sql = "insert into db_book.dbo.t_user values(?,?,?,?,?)";
			ps=con.prepareStatement(sql);
			ps.setString(1, map.get("user")[0]);
			ps.setString(2, map.get("password")[0]);
			ps.setString(3, map.get("Name")[0]);
			ps.setString(4, map.get("sex")[0]);
			ps.setString(5, map.get("tele")[0]);
			int count = ps.executeUpdate();
			if(count>0)
			{
				req.getRequestDispatcher("index.jsp").forward(req, resp);
			}else {
				req.getRequestDispatcher("erroruser.jsp").forward(req, resp);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doGet(req, resp);
	}

}
